package binaryTree.LayerOrderTraversal;

import java.util.LinkedList;
import java.util.Queue;
//.填充每个节点的下一个右侧节点指针II
public class Basic177 {
    public Node1 connect(Node1 root) {
       Queue<Node1> que = new LinkedList<>();
       if (root!=null){
           que.add(root);
       }
       while (!que.isEmpty()){
           int size = que.size();
           Node1 cur = que.poll();
           if (cur.left!=null){
               que.add(cur.left);
           }
           if (cur.right!=null){
               que.add(cur.right);
           }
           for (int index = 1; index < size; index++) {
               Node1 next = que.poll();
               if (next.left!=null){
                   que.add(next.left);
               }
               if (next.right!=null){
                   que.add(next.right);
               }
               cur.next=next;
               cur=next;

           }

       }
        return  root;

    }
}
